/*
 * Copyright (c) 1994, 2021, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package com.i360day.invoker.configuration;

import com.i360day.invoker.InvokerBasicConfiguration;
import com.i360day.invoker.WebSocketHttpInvokerRequest;
import com.i360day.invoker.interceptor.HttpInvokerRequestInterceptor;
import com.i360day.invoker.properties.HttpInvokerProperties;
import com.i360day.invoker.registry.RemoteScanDefinitionRegistryPostProcessor;
import com.i360day.invoker.registry.WebSocketRemoteScanImportBeanDefinitionRegistrar;
import com.i360day.invoker.request.HttpInvokerRequest;
import com.i360day.invoker.socket.SimpleWebSocketTemplate;
import com.i360day.invoker.socket.WebSocketHttpRequestHandler;
import com.i360day.invoker.socket.handler.SimpleWebSocketEndpoint;
import com.i360day.invoker.socket.handler.SimpleWebSocketHandlerMapping;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerMapping;

import java.util.Collections;
import java.util.Map;

/**
 * implements WebSocketConfigurer
 *
 * @author: liju.z
 * @date: 2023/12/4 14:07
 */
@Configuration(proxyBeanMethods = false)
@ImportAutoConfiguration(InvokerBasicConfiguration.class)
public class HttpInvokerWebSocketConfiguration {

    @Autowired
    private HttpInvokerProperties httpInvokerProperties;

    /**
     * 注册socket
     *
     * @return
     */
    @Bean
    public HandlerMapping httpInvokerWebSocketHandlerMapping(WebSocketHttpRequestHandler webSocketHttpRequestHandler) {
        Map<String, WebSocketHttpRequestHandler> urlParams = Collections.singletonMap(httpInvokerProperties.getWebSocket().getEndpoint(), webSocketHttpRequestHandler);
        return new SimpleWebSocketHandlerMapping(urlParams, 1);
    }

    @Bean
    @ConditionalOnMissingBean
    public WebSocketHttpRequestHandler webSocketHttpRequestHandler(SimpleWebSocketEndpoint simpleWebSocketEndpoint) {
        return new WebSocketHttpRequestHandler(simpleWebSocketEndpoint);
    }

    @Bean
    @ConditionalOnMissingBean
    public SimpleWebSocketEndpoint simpleWebSocketEndpoint(SimpleWebSocketTemplate simpleWebSocketTemplate) {
        return new SimpleWebSocketEndpoint(simpleWebSocketTemplate);
    }

    @Bean
    @ConditionalOnMissingBean
    public SimpleWebSocketTemplate webSocketTemplate() {
        return new SimpleWebSocketTemplate(httpInvokerProperties);
    }

    @Bean
    @ConditionalOnMissingBean
    public HttpInvokerRequest httpInvokerRequest(SimpleWebSocketTemplate simpleWebSocketTemplate) throws Exception {
        return new WebSocketHttpInvokerRequest(simpleWebSocketTemplate, httpInvokerProperties);
    }

    @Bean
    @ConditionalOnMissingBean
    public RemoteScanDefinitionRegistryPostProcessor remoteBeanDefinitionRegistryPostProcessor() {
        return new RemoteScanDefinitionRegistryPostProcessor() {
            @Override
            public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
                setRegistrarFactory(new WebSocketRemoteScanImportBeanDefinitionRegistrar(getResourceLoader(), getEnvironment()));
                super.postProcessBeanDefinitionRegistry(registry);
            }
        };
    }

    /**
     * <p> @author liju.z <p>
     *
     * <p> @Description 创建默认请求拦截器 <p>
     *
     * <p> @Date  21:03 <p>
     *
     * <p> @return HttpInvokerRequestInterceptor <p>
     **/
    @Bean
    @ConditionalOnMissingBean
    public HttpInvokerRequestInterceptor simpleRequestInterceptor() {
        return requestTemplate -> {

        };
    }
}
